/**
 * 果篮item
 */
class FruitBasketItem extends BaseComp{
    public img_bg:eui.Image;
    private img_new_bg:eui.Image;
    private group_1:eui.Group;
    private group_2:eui.Group;
    private group_3:eui.Group;
    
    private img_icon_1:eui.Image;
    private img_icon_2:eui.Image;
    private img_icon_3:eui.Image;
    private img_icon_4:eui.Image;

    private lb_money_1:eui.Label;
    private lb_money_2:eui.Label;
    private lb_money_3:eui.Label;
    private lb_money_4:eui.Label;

    private group_item:eui.Group;
    private img_icon:eui.Image;

    private index;
    constructor()
    {
        super('FruitBasketItemView');
    }

    public init()
    {
        let index = Math.floor(Math.random() * 3 + 1);
        this.img_bg.source = 'basket_bg_'+index;
    }

    public initUI(data)
    {
        this.data = data;
        if(this.data)
        {
            let award:string = this.data;
            let awardArr = award.split(',');
            if(awardArr.length == 1)
            {
                let awardData = ResourceUtil.instance().parseSignleAward(award);
                
                if(awardData.itemId == resourceType.gold)
                {
                    this.img_icon_1.source = awardData.itemIcon;
                    this.lb_money_1.text = 'x'+awardData.itemNum;
                    this.img_icon.source = 'basket_new_4';
                    this.index = 1;
                }
                else
                {
                    this.img_icon_2.source = awardData.itemIcon;
                    this.lb_money_2.text = 'x'+awardData.itemNum;
                    this.img_icon.source = 'basket_new_5';
                    this.index = 2;
                }   
            }
            else
            {
                let awardData1 = ResourceUtil.instance().parseSignleAward(awardArr[0]);
                this.img_icon_3.source = awardData1.itemIcon;
                this.lb_money_3.text = 'x'+awardData1.itemNum;

                let awardData2 = ResourceUtil.instance().parseSignleAward(awardArr[1]);
                this.img_icon_4.source = awardData2.itemIcon;
                this.lb_money_4.text = 'x'+awardData2.itemNum;

                this.img_icon.source = 'basket_new_6'
                this.index = 3;
            }
        }
    }

    public addEvent()
    {
        this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.touchHandle, this);
    }

    protected touchHandle()
    {
        let logic:FruitBasketUI = UIManager.instance().getLogicByName(UIConst.FRUITBASKET_UI) as FruitBasketUI;
        if(logic)
        {
            if(logic.isCanTouch)
            {
                let frame = 100/3;
                let time = 7 * frame;
                let self = this;

                egret.Tween.get(self).to({
                    scaleX:0,
                },time).call(()=>{
                    // self.img_bg.source = 'basket_bg_4';
                    self.img_bg.visible = false;
                    self.img_new_bg.visible = true;
                    self.group_item.visible = true;
                    self['group_'+self.index].visible = true;
                }).
                to({scaleX:1,},time).wait(300).call(()=>{
                    self.getAward();
                })
                logic.isCanTouch = false;
            }
            else
            {
                MessageUtil.tips('已经选择过奖励了哦~')
            }
        }
    }

    public getAward()
    {
        let data = {
            awards:this.data,
            version:GameConst.version
        }
        
        HttpManager.instance().sendPost(GameConst.httpUrl + 'warriorother/do_lucky_card', StringUtil.jsonToHttpPostString(data), (e)=>{
            let data = JSON.parse(e)
            if(data.msg == 'ok')
            {
                GameUtil.openAwardUI(this.data, null);
                let logic:FruitBasketUI = UIManager.instance().getLogicByName(UIConst.FRUITBASKET_UI) as FruitBasketUI;
                if(logic)
                {
                    logic.close();
                }
            }
        }, this);
    }

    public removeEvent()
    {
        this.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.touchHandle, this)
    }

    public clear()
    {
        super.clear();
        this.index = null;
    }
}

window['FruitBasketItem'] = FruitBasketItem;